package gui.tools;

import gui.Frame;
import gui.panels.AddLocation;
import gui.panels.AddMovie;
import gui.panels.AddMusic;
import gui.panels.AddPerson;
import gui.panels.AddSeries;
import gui.panels.CheckoutMovie;
import gui.panels.CheckoutMusic;
import gui.panels.CheckoutSeries;
import gui.panels.LocationPanel;
import gui.panels.MoviePanel;
import gui.panels.MusicPanel;
import gui.panels.PersonPanel;
import gui.panels.SeriesPanel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ActionHandler implements ActionListener {

	private Frame main;
	private PersonPanel pp;
	private MoviePanel mm;
	private MusicPanel mu;
	private SeriesPanel se;
	private LocationPanel lo;

	public ActionHandler(Frame main) {
		this.main = main;
	}

	public void getPersonAddtions(PersonPanel pp) {
		this.pp = pp;
	}

	public void getMovieAddtions(MoviePanel mm) {
		this.mm = mm;
	}

	public void getMusicAdditions(MusicPanel mu) {
		this.mu = mu;
	}

	public void getSeriesAdditions(SeriesPanel se) {
		this.se = se;
	}
	

	public void getLocationAdditions(LocationPanel lo) {
		this.lo = lo;
	}


	@Override
	public void actionPerformed(ActionEvent ev) {

		if (ev.getActionCommand() == main.rPersons.getText()) {
			main.remove(main.ppan);
			main.remove(main.mupan);
			main.remove(main.sepan);
			main.remove(main.mpan);
			main.remove(main.lopan);
			main.remove(main.cupan);
			main.add(main.ppan, BorderLayout.CENTER);
			main.isEnabled = false;
			main.setJMenuBar(main.generateMenu(this));
			pp.pCurrentEntryIndex = 0;
			mm.mCurrentEntryIndex = 0;
			mu.muCurrentEntryIndex = 0;
			se.sCurrentEntryIndex = 0;
			lo.locCurrentEntryIndex = 0;
			getAllPersonData();
			main.repaint();
			main.validate();
		}

		if (ev.getActionCommand() == main.rMovies.getText()) {
			main.remove(main.ppan);
			main.remove(main.mupan);
			main.remove(main.sepan);
			main.remove(main.mpan);
			main.remove(main.lopan);
			main.remove(main.cupan);
			main.add(main.mpan, BorderLayout.CENTER);
			main.isEnabled = false;
			main.setJMenuBar(main.generateMenu(this));
			pp.pCurrentEntryIndex = 0;
			mm.mCurrentEntryIndex = 0;
			mu.muCurrentEntryIndex = 0;
			se.sCurrentEntryIndex = 0;
			lo.locCurrentEntryIndex = 0;
			getAllMovieData();
			main.repaint();
			main.validate();
		}

		if (ev.getActionCommand() == main.rMusic.getText()) {
			main.remove(main.ppan);
			main.remove(main.mupan);
			main.remove(main.sepan);
			main.remove(main.mpan);
			main.remove(main.lopan);
			main.remove(main.cupan);
			main.add(main.mupan, BorderLayout.CENTER);
			main.isEnabled = false;
			main.setJMenuBar(main.generateMenu(this));
			pp.pCurrentEntryIndex = 0;
			mm.mCurrentEntryIndex = 0;
			mu.muCurrentEntryIndex = 0;
			se.sCurrentEntryIndex = 0;
			lo.locCurrentEntryIndex = 0;
			getAllMusicData();
			main.repaint();
			main.validate();
		}

		if (ev.getActionCommand() == main.rSeries.getText()) {
			main.remove(main.ppan);
			main.remove(main.mupan);
			main.remove(main.sepan);
			main.remove(main.mpan);
			main.remove(main.lopan);
			main.remove(main.cupan);
			main.add(main.sepan, BorderLayout.CENTER);
			main.isEnabled = false;
			main.setJMenuBar(main.generateMenu(this));
			se.sCurrentEntryIndex = 0;
			pp.pCurrentEntryIndex = 0;
			mm.mCurrentEntryIndex = 0;
			mu.muCurrentEntryIndex = 0;
			lo.locCurrentEntryIndex = 0;
			getAllSeriesData();
			main.repaint();
			main.validate();
		}
		
		if (ev.getActionCommand() == main.rLocations.getText()) {
			main.remove(main.ppan);
			main.remove(main.mupan);
			main.remove(main.sepan);
			main.remove(main.mpan);
			main.remove(main.lopan);
			main.remove(main.cupan);
			main.add(main.lopan, BorderLayout.CENTER);
			main.isEnabled = false;
			main.setJMenuBar(main.generateMenu(this));
			se.sCurrentEntryIndex = 0;
			pp.pCurrentEntryIndex = 0;
			mm.mCurrentEntryIndex = 0;
			mu.muCurrentEntryIndex = 0;
			lo.locCurrentEntryIndex = 0;
			getAllLocationsData();
			main.repaint();
			main.validate();
		}
		
		if (ev.getActionCommand() == main.rCustom.getText()) {
			main.remove(main.ppan);
			main.remove(main.mupan);
			main.remove(main.sepan);
			main.remove(main.mpan);
			main.remove(main.lopan);
			main.remove(main.cupan);
			main.add(main.cupan, BorderLayout.CENTER);
			main.isEnabled = true;
			main.setJMenuBar(main.generateMenu(this));
			main.repaint();
			main.validate();
		}

		if (ev.getSource() == pp.next) {
			if (pp.pCurrentEntryIndex > pp.pResults.size() - 1) {
				pp.pCurrentEntryIndex = pp.pResults.size();
				System.out.println("Index over maxx");
			} else {
				pp.pCurrentEntryIndex++;
				try {
					getAllPersonData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more users");
				}
			}
		}
		if (ev.getSource() == pp.previous) {
			if (pp.pCurrentEntryIndex < 0) {
				pp.pCurrentEntryIndex = 0;
				System.out.println("Index under 0");
			} else {
				pp.pCurrentEntryIndex--;
				try {
					getAllPersonData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more users");
				}
			}
		}

		if (ev.getSource() == mm.next) {
			if (mm.mCurrentEntryIndex > mm.mResults.size()- 1) {
				mm.mCurrentEntryIndex = mm.mResults.size();
				System.out.println("Index over maxx");
			} else {
				mm.mCurrentEntryIndex++;
				try {
					if (mm.mResults.get(mm.mCurrentEntryIndex).isOut() == 1) {
						mm.outText.setText("True");
					} else if (mm.mResults.get(mm.mCurrentEntryIndex).isOut() == 0) {
						mm.outText.setText("False");
					}
					getAllMovieData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more movies");
				}
			}
		}
		if (ev.getSource() == mm.previous) {
			if (mm.mCurrentEntryIndex < 0) {
				mm.mCurrentEntryIndex = 0;
				System.out.println("Index under 0");
			} else {
				mm.mCurrentEntryIndex--;
				try {
					getAllMovieData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more movies");
				}
			}
		}
		if (ev.getSource() == mu.next) {
			if (mu.muCurrentEntryIndex > mu.muResults.size() -1 ) {
				mu.muCurrentEntryIndex = mu.muResults.size();
				System.out.println("Index over maxx");
			} else {
				mu.muCurrentEntryIndex++;
				try {
					if (mm.mResults.get(mm.mCurrentEntryIndex).isOut() == 1) {
						mm.outText.setText("True");
					} else if (mm.mResults.get(mm.mCurrentEntryIndex).isOut() == 0) {
						mm.outText.setText("False");
					}
					getAllMusicData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more music");
				}
			}
		}
		if (ev.getSource() == mu.previous) {
			if (mu.muCurrentEntryIndex < 0) {
				mu.muCurrentEntryIndex = 0;
				System.out.println("Index under 0");
			} else {
				mu.muCurrentEntryIndex--;
				try {
					getAllMusicData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more music");
				}
			}
		}
		if (ev.getSource() == se.next) {
			if (se.sCurrentEntryIndex > se.sResults.size() - 1) {
				se.sCurrentEntryIndex = se.sResults.size();
				System.out.println("Index over maxx");
			} else {
				se.sCurrentEntryIndex++;
				try {
					if (mm.mResults.get(mm.mCurrentEntryIndex).isOut() == 1) {
						mm.outText.setText("True");
					} else if (mm.mResults.get(mm.mCurrentEntryIndex).isOut() == 0) {
						mm.outText.setText("False");
					}
					getAllSeriesData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more series");
				}
			}
		}
		if (ev.getSource() == se.previous) {
			if (se.sCurrentEntryIndex < 0) {
				se.sCurrentEntryIndex = 0;
				System.out.println("Index under 0");
			} else {
				se.sCurrentEntryIndex--;
				try {
					getAllSeriesData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more series");
				}
			}
		}
		
		if (ev.getSource() == lo.next) {
			if (lo.locCurrentEntryIndex > lo.locResults.size() - 1) {
				lo.locCurrentEntryIndex = lo.locResults.size();
				System.out.println("Index over maxx");
			} else {
				lo.locCurrentEntryIndex++;
				try {
					getAllLocationsData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more locations");
				}
			}
		}
		if (ev.getSource() == lo.previous) {
			if (lo.locCurrentEntryIndex < 0) {
				lo.locCurrentEntryIndex = 0;
				System.out.println("Index under 0");
			} else {
				lo.locCurrentEntryIndex--;
				try {
					getAllLocationsData();
				} catch (IndexOutOfBoundsException ie) {
					System.out.println("No more locations");
				}
			}
		}


		// Add user function
		if (ev.getSource() == pp.add) {
			System.out.println("Add User");
			AddPerson addP = new AddPerson();
		}

		// Add Movie function
		if (ev.getSource() == mm.add) {
			System.out.println("Add Movie");
			AddMovie addM = new AddMovie();
		}
		
		//Add Location function
		if (ev.getSource() == lo.add) {
			System.out.println("Add Location");
			AddLocation addL = new AddLocation();
		}
		
		//Add music function
		if (ev.getSource() == mu.add) {
			System.out.println("Add Music");
			AddMusic addMu = new AddMusic();
		}
		
		//Add series function
		if (ev.getSource() == se.add) {
			System.out.println("Add series");
			AddSeries addSe = new AddSeries();
		}
		
		if (ev.getActionCommand() == "Exit") {
			System.out.println("Exiting");
			System.exit(0);
		}
		
		if (ev.getActionCommand() == "Checkout Movie") {
			System.out.println("Checkout Movie");
			CheckoutMovie coMovie = new CheckoutMovie();
		}
		
		if (ev.getActionCommand() == "Checkout Music") {
			System.out.println("Checkout Music");
			CheckoutMusic coMusic = new CheckoutMusic();
		}
		
		if (ev.getActionCommand() == "Checkout Series") {
			System.out.println("Checkout Series");
			CheckoutSeries coSeries = new CheckoutSeries();
		}
		
		if (ev.getActionCommand() == "Query 1") {
			main.cupan.input.setText("");
			main.cupan.input.setText("SELECT title, state FROM Movie WHERE `state` = \"internal\" OR `state` = \"external\" UNION SELECT album, state FROM Music WHERE `state` = \"internal\" OR `state` = \"external\" UNION SELECT title, state FROM Series WHERE `state` = \"internal\" OR `state` = \"external\"");
			main.cupan.repaint();
			main.cupan.validate();
		}
		
		if (ev.getActionCommand() == "Query 2") {
			main.cupan.input.setText("");
			main.cupan.input.setText("SELECT Mo.title, Mo.date, P.lastname FROM Movie Mo JOIN Person P ON Mo.outTo = P.pid WHERE `out` = true UNION SELECT Mu.album, Mu.date, P.lastname FROM Music Mu JOIN Person P ON Mu.outTo = P.pid WHERE `out` = true UNION SELECT Se.title, Se.date, P.lastname FROM Series Se JOIN Person P ON Se.outTo = P.pid WHERE `out` = true");
			main.cupan.repaint();
			main.cupan.validate();
		}
		
		if (ev.getActionCommand() == "Query 3") {
			main.cupan.input.setText("");
			main.cupan.input.setText("SELECT P.forname, P.lastname, M.title FROM Person P JOIN Movie M ON M.outTo = P.pid WHERE `out` = true UNION SELECT P.forname, P.lastname, Mu.album FROM Person P JOIN Music Mu ON Mu.outTo = P.pid WHERE `out` = true UNION SELECT P.forname, P.lastname, S.title FROM Person P JOIN Series S ON S.outTo = P.pid WHERE `out` = true");
			main.cupan.repaint();
			main.cupan.validate();
		}
		
		if (ev.getActionCommand() == "Query 4") {
			main.cupan.input.setText("");
			main.cupan.input.setText("SELECT P.forname, P.lastname, L.address, L.state, M.title FROM Person P JOIN Locations L ON P.lid = L.lid JOIN Movie M ON M.location = L.lid");
			main.cupan.repaint();
			main.cupan.validate();
		}
	}

	public void getAllPersonData() {
		pp.pcurrentEntry = pp.pResults.get(pp.pCurrentEntryIndex);
		pp.index.setText(Integer.toString((pp.pCurrentEntryIndex) + 1));
		pp.pidText.setText(Integer.toString(pp.pcurrentEntry.getPid()));
		pp.firstnameText.setText(pp.pcurrentEntry.getFirstName());
		pp.lastnameText.setText(pp.pcurrentEntry.getLastName());
		pp.locationText.setText(mm.locResults.get(pp.pCurrentEntryIndex).getAddress());
	}
	
	public void getAllLocationsData() {
		lo.loccurrentEntry = lo.locResults.get(lo.locCurrentEntryIndex);
		lo.index.setText((Integer.toString((lo.locCurrentEntryIndex) + 1) ));
		lo.addressText.setText(lo.loccurrentEntry.getAddress());
		lo.stateText.setText(lo.loccurrentEntry.getState());
	}

	public void getAllMovieData() {
		mm.mcurrentEntry = mm.mResults.get(mm.mCurrentEntryIndex);
		mm.index.setText(Integer.toString((mm.mCurrentEntryIndex) + 1));
		mm.moidText.setText(Integer.toString(mm.mcurrentEntry.getMoid()));
		mm.titleText.setText(mm.mcurrentEntry.getTitle());
		mm.yearText.setText(Integer.toString(mm.mcurrentEntry.getYear()));
		mm.lidText.setText(mm.locResults.get(mm.mCurrentEntryIndex).getAddress());
		if (mm.mResults.get(mm.mCurrentEntryIndex).isOut() == 1) {
			mm.outText.setText("True");
		} else 	mm.outText.setText("False");
	}

	public void getAllMusicData() {
		mu.mucurrentEntry = mu.muResults.get(mu.muCurrentEntryIndex);
		mu.index.setText(Integer.toString((mu.muCurrentEntryIndex) + 1));
		mu.muidText.setText(Integer.toString(mu.mucurrentEntry.getMuid()));
		mu.artistText.setText(mu.mucurrentEntry.getArtist());
		mu.albumText.setText(mu.mucurrentEntry.getAlbum());
		mu.lidText.setText(Integer.toString(mu.mucurrentEntry.getLocation()));
		if (mm.mResults.get(mm.mCurrentEntryIndex).isOut() == 1) {
			mm.outText.setText("True");
		} else 	mm.outText.setText("False");
	}

	public void getAllSeriesData() {
		se.scurrentEntry = se.sResults.get(se.sCurrentEntryIndex);
		se.index.setText(Integer.toString((se.sCurrentEntryIndex) + 1));
		se.seidText.setText(Integer.toString(se.scurrentEntry.getSeid()));
		se.titleText.setText(se.scurrentEntry.getTitle());
		se.seasonText.setText(Integer.toString(se.scurrentEntry.getSeason()));
		se.episodeText.setText(se.scurrentEntry.getEpisode());
		se.lidText.setText(Integer.toString(se.scurrentEntry.getLocation()));
		if (mm.mResults.get(mm.mCurrentEntryIndex).isOut() == 1) {
			mm.outText.setText("True");
		} else 	mm.outText.setText("False");
	}

}
